/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package net.srt.flink.gateway.kubernetes;

import lombok.SneakyThrows;
import net.srt.flink.common.assertion.Asserts;
import net.srt.flink.common.model.ProjectSystemConfiguration;
import net.srt.flink.gateway.GatewayType;
import net.srt.flink.gateway.config.AppConfig;
import net.srt.flink.gateway.exception.GatewayException;
import net.srt.flink.gateway.result.GatewayResult;
import net.srt.flink.gateway.result.KubernetesResult;
import net.srt.flink.process.context.ProcessContextHolder;
import net.srt.flink.process.model.ProcessEntity;
import org.apache.flink.client.deployment.ClusterSpecification;
import org.apache.flink.client.deployment.application.ApplicationConfiguration;
import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.client.program.ClusterClientProvider;
import org.apache.flink.configuration.JobManagerOptions;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.configuration.TaskManagerOptions;
import org.apache.flink.kubernetes.KubernetesClusterDescriptor;
import org.apache.flink.runtime.client.JobStatusMessage;
import org.apache.flink.runtime.jobgraph.JobGraph;
import org.apache.http.util.TextUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * KubernetesApplicationGateway
 *
 * @author zrx
 * @since 2021/12/26 14:59
 */
public class KubernetesApplicationGateway extends KubernetesGateway {
	@Override
	public GatewayType getType() {
		return GatewayType.KUBERNETES_APPLICATION;
	}

	@Override
	public GatewayResult submitJobGraph(JobGraph jobGraph) {
		throw new GatewayException("Couldn't deploy Kubernetes Application Cluster with job graph.");
	}

	@SneakyThrows
	@Override
	public GatewayResult submitJar() {
		if (Asserts.isNull(client)) {
			init();
		}
		KubernetesResult result = KubernetesResult.build(getType());
		AppConfig appConfig = config.getAppConfig();
		configuration.set(PipelineOptions.JARS, Collections.singletonList(appConfig.getUserJarPath()));
		String[] userJarParas = appConfig.getUserJarParas();
		if (Asserts.isNull(userJarParas)) {
			userJarParas = new String[0];
		}
		ApplicationConfiguration applicationConfiguration = new ApplicationConfiguration(userJarParas, appConfig.getUserJarMainAppClass());
		KubernetesClusterDescriptor kubernetesClusterDescriptor = new KubernetesClusterDescriptor(configuration, client);

		ClusterSpecification.ClusterSpecificationBuilder clusterSpecificationBuilder = new ClusterSpecification.ClusterSpecificationBuilder();
		if (configuration.contains(JobManagerOptions.TOTAL_PROCESS_MEMORY)) {
			clusterSpecificationBuilder.setMasterMemoryMB(configuration.get(JobManagerOptions.TOTAL_PROCESS_MEMORY).getMebiBytes());
		}
		if (configuration.contains(TaskManagerOptions.TOTAL_PROCESS_MEMORY)) {
			clusterSpecificationBuilder.setTaskManagerMemoryMB(configuration.get(TaskManagerOptions.TOTAL_PROCESS_MEMORY).getMebiBytes());
		}
		if (configuration.contains(TaskManagerOptions.NUM_TASK_SLOTS)) {
			clusterSpecificationBuilder.setSlotsPerTaskManager(configuration.get(TaskManagerOptions.NUM_TASK_SLOTS)).createClusterSpecification();
		}

		try {
			ClusterClientProvider<String> clusterClientProvider = kubernetesClusterDescriptor.deployApplicationCluster(
					clusterSpecificationBuilder.createClusterSpecification(), applicationConfiguration);
			ClusterClient<String> clusterClient = clusterClientProvider.getClusterClient();
			Collection<JobStatusMessage> jobStatusMessages = clusterClient.listJobs().get();
			//zrx ProjectSystemConfiguration
			ProcessEntity process = ProcessContextHolder.getProcess();
			int counts = ProjectSystemConfiguration.getByProjectId(process.getProjectId()).getJobIdWait();
			while (jobStatusMessages.size() == 0 && counts > 0) {
				Thread.sleep(1000);
				counts--;
				jobStatusMessages = clusterClient.listJobs().get();
				if (jobStatusMessages.size() > 0) {
					break;
				}
			}
			if (jobStatusMessages.size() > 0) {
				List<String> jids = new ArrayList<>();
				for (JobStatusMessage jobStatusMessage : jobStatusMessages) {
					jids.add(jobStatusMessage.getJobId().toHexString());
				}
				result.setJids(jids);
			}
			String jobId = "";
			//application mode only have one job, so we can get any one to be jobId
			for (JobStatusMessage jobStatusMessage : jobStatusMessages) {
				jobId = jobStatusMessage.getJobId().toHexString();
			}
			//if JobStatusMessage not have job id, use timestamp
			//and... it`s maybe wrong with submit
			if (TextUtils.isEmpty(jobId)) {
				jobId = "unknown" + System.currentTimeMillis();
			}
			result.setClusterId(jobId);
			result.setWebURL(clusterClient.getWebInterfaceURL());
			result.success();
			// zrx
		} /*catch (Exception e) {
			result.fail(LogUtil.getError(e));
		}*/ finally {
			kubernetesClusterDescriptor.close();
		}
		return result;
	}
}
